package com.qiantu.phone.ui.fragment.device;

import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.db.LLDeviceCustomDBManager;
import com.qiantu.api.entity.DeviceCustomKeyBean;
import com.qiantu.api.entity.DeviceCustomNewBean;
import com.qiantu.api.entity.DeviceCustomNewBeanKeyBindBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.app.AppApplication;

import java.util.List;

import okhttp3.Call;

public class KeyDeviceFragment extends BaseDeviceFragment{
    @Override
    protected int getLayoutId() {
        return 0;
    }

    @Override
    protected void initView() {
        super.initView();
        List<DeviceCustomKeyBean> beans = LLDeviceCustomDBManager.getInstance(getContext()).getDataByDeviceSerialNo(mDevice.getDeviceSerialNo());
        mDevice.setDeviceCustomKeys(beans);
        getDeviceCustomKeyList();
    }

    @Override
    public void onResume(){
        super.onResume();
        List<DeviceCustomKeyBean> beans = LLDeviceCustomDBManager.getInstance(getContext()).getDataByDeviceSerialNo(mDevice.getDeviceSerialNo());
        mDevice.setDeviceCustomKeys(beans);
        updateCustomKey(beans);
        getDeviceCustomKeyList();
    }


    public void updateCustomKey(List<DeviceCustomKeyBean> customKeyList){}

    //新加名字的
    public void updateCustomKeyNew(List<DeviceCustomNewBean> customKeyList){}

    protected void getDeviceCustomKeyList(){
        LLHttpManager.getCustomKeyListBySerialNo(this, AppApplication.getInstance().getCurHouseInfo().getHouseSerialNo(), mDevice.getDeviceSerialNo(), new HttpCallback<HttpData<List<DeviceCustomNewBean>>>(null) {
            @Override
            public void onStart(Call call) {

            }

            @Override
            public void onSucceed(HttpData<List<DeviceCustomNewBean>> result) {
                List<DeviceCustomNewBean> customKeyList = result.getData();
                updateCustomKeyNew(customKeyList);
            }

            @Override
            public void onFail(Exception e) {

            }

            @Override
            public void onEnd(Call call) {
            }
        });
    }

    protected String getBingKeyName(DeviceCustomNewBean keyData) {
        List<DeviceCustomNewBeanKeyBindBean> binds = keyData.getKeyBindList();
        StringBuilder sub = new StringBuilder();  // 使用 StringBuilder 来拼接字符串
        if (binds != null) {
            for (DeviceCustomNewBeanKeyBindBean keyBind : binds) {
                if (keyBind.getKeyOperate() == 2) {
                    sub.append("双击").append(keyBind.getBindActionName());
                } else if (keyBind.getKeyOperate() == 13) {
                    sub.append("长按").append(keyBind.getBindActionName());
                }
            }
        }
        return sub.toString();  // 返回拼接后的字符串
    }
}
